﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DataInfo;
using KenLib.BusinessManager;
using System.Collections.Generic;
using KenLib.BusinessObject;

public partial class JOINT_frmJ_TraineeResumeAInput : System.Web.UI.Page
{
    国籍Manager _国籍Manager = new 国籍Manager();
    応募情報Manager _応募情報Manager = new 応募情報Manager();
    
    /// <summary>
    /// Coder: Thanh Dong
    /// Event: Page load
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        ((JOINT_MstPage_JOINT)this.Master).HideLiArea("frmJ_TraineeResumeAInput");
        ((JOINT_MstPage_JOINT)this.Master).changeTitle("来社後　情報確認・修正");
        txt_姓.Focus();
        if (!IsPostBack)
        {
            if (!string.IsNullOrEmpty(Request.Params["id"]))
            {
                int dem = 0;
                Int32.TryParse(Request.Params["id"], out dem);
                if (dem != 0)
                {
                    Show_国籍();
                    LoadData(dem);
                }
                else
                {
                    Response.Redirect("frmJ_TraineeInterviewList.aspx");
                }
            }
            else
            {
                Response.Redirect("frmJ_TraineeInterviewList.aspx");
            }
            
        }
        RFV_生年月日.ErrorMessage = GetError("ER00001");
        CV_生年月日.ErrorMessage = GetError("ER00005");
        RFV_ＩＤ番号発行日.ErrorMessage = GetError("ER00001");
        CV_ＩＤ番号発行日.ErrorMessage = GetError("ER00005");
        CV_ＩＤ番号発行日1.ErrorMessage = GetError("ER00005");
        CV_ＩＤ番号発行日2.ErrorMessage = GetError("ER00005");
        RFV_姓.ErrorMessage =  GetError("ER00001");
        RFV_名.ErrorMessage =  GetError("ER00001");
        RFV_出生地.ErrorMessage = GetError("ER00001");
        RFV_ID番号.ErrorMessage = GetError("ER00001");
        RFV_発行機関.ErrorMessage = GetError("ER00001");
        RFV_宗教.ErrorMessage = GetError("ER00001");
        RFV_民族.ErrorMessage = GetError("ER00001");
        RFV_最終学歴.ErrorMessage = GetError("ER00007");
        RFV_ddl_性別.ErrorMessage = GetError("ER00007");
        RFV_ddl_国籍.ErrorMessage = GetError("ER00007");
    }
    
    /// <summary>
    /// Coder: Thanh Dong
    /// Event: go to frmJ_TraineeInterviewList.aspx
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnキャンセル_Click(object sender, EventArgs e)
    {
        HttpContext.Current.Session.Remove("ID_frmJ_TraineeResumeAInput");
        Response.Redirect("frmJ_TraineeInterviewList.aspx");
    }
    
    /// <summary>
    /// Coder: Thanh Dong
    /// Event: Update database for table 学歴情報登録
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn学歴情報登録_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request.Params["id"]))
        {
            int id = 0;
            Int32.TryParse(Request.Params["id"], out id);
            if (id != 0)
            {
                if (frmJ_TraineeResumeAInput_Update(id) == 1)
                {
                    Session["ID_frmJ_TraineeResumeAInput"] = id;
                    Response.Redirect("frmJ_TraineeAcademicInput.aspx");
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('データベースに接続ができません');</script>");
                }
            }
        }
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Get error message from id
    /// </summary>
    /// <param name="errorid"></param>
    /// <returns></returns>
    public String GetError(string errorid)
    {
        string error = "";
        Error er = new Error();
        er.ErrorID = errorid;
        ErrorManager frm_error = new ErrorManager();
        DataTable dt = frm_error.getError(er);
        if (dt.Rows.Count > 0)
            error = dt.Rows[0][2].ToString();
        return error;
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Load data for dropdownlist 国籍
    /// </summary>
    public void Show_国籍()
    {
        ddl_国籍.DataSource = _国籍Manager.Select();
        ddl_国籍.DataTextField = "国籍名";
        ddl_国籍.DataValueField = "ID";
        ddl_国籍.DataBind();
        ListItem item = new ListItem("[選択]", "-1");
        item.Selected = true;
        ddl_国籍.Items.Insert(0, item);
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Load data for all control page
    /// </summary>
    /// <param name="id"></param>
    public void LoadData(int id)
    {
        DataTable dt = new DataTable();
        dt = _応募情報Manager.frmJ_TraineeResumeAInput_SelectByID(id);
        if (dt.Rows.Count > 0)
        {
            foreach (DataRow row in dt.Rows)
            {
                txt_名.Text = ConvertTexttoHTML(row["名"].ToString());
                txt_姓.Text = ConvertTexttoHTML(row["姓"].ToString());


                if (row["性別"].ToString() == "True")
                {
                    ListItem lst = ddl_性別.Items.FindByText("女");
                    ddl_性別.SelectedIndex = ddl_性別.Items.IndexOf(lst);
                }
                else
                {
                    ListItem lst = ddl_性別.Items.FindByText("男");
                    ddl_性別.SelectedIndex = ddl_性別.Items.IndexOf(lst);
                }

                //String.Format("{0:d/M/yyyy HH:mm:ss}", dt);
                if (row["生年月日"].ToString() != "")
                {
                    txt_生年月日.Text = String.Format("{0:yyyy/MM/dd}", Convert.ToDateTime(row["生年月日"]));
                }
                if (row["国籍"].ToString() != "")
                {
                    ListItem lst = ddl_国籍.Items.FindByValue(row["国籍"].ToString());
                    ddl_国籍.SelectedIndex = ddl_国籍.Items.IndexOf(lst);
                }
                txt_出生地.Text = ConvertTexttoHTML(row["出身地"].ToString());
                txt_ID番号.Text = ConvertTexttoHTML(row["ID番号"].ToString());
                if (row["ＩＤ番号発行日"].ToString() != "")
                {
                    txt_ＩＤ番号発行日.Text = String.Format("{0:yyyy/MM/dd}", Convert.ToDateTime(row["ＩＤ番号発行日"]));
                }
                txt_発行機関.Text = ConvertTexttoHTML(row["発行機関"].ToString());
                txt_宗教.Text = ConvertTexttoHTML(row["宗教"].ToString());
                txt_民族.Text = ConvertTexttoHTML(row["民族"].ToString());
                if (row["最終学歴"].ToString() != "")
                {
                    //ListItem lst = ddl_国籍.Items.FindByValue(row["国籍"].ToString());
                    //ddl_国籍.SelectedIndex = ddl_国籍.Items.IndexOf(lst);
                    ListItem lst = ddl_最終学歴.Items.FindByValue(row["最終学歴"].ToString());
                    ddl_最終学歴.SelectedIndex = ddl_最終学歴.Items.IndexOf(lst);
                }
                //txt_最終学歴.Text = ConvertTexttoHTML(row["最終学歴"].ToString());

            }
        }
        else
        {
            Response.Redirect("frmJ_TraineeInterviewList.aspx");
        }
       
      
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Get object 応募情報
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public 応募情報 Get応募情報(int id)
    {
       
        応募情報 _応募情報 = new 応募情報();
        _応募情報.ID = id;
        _応募情報.姓 = ConvertHTMLtoText(txt_姓.Text);
        _応募情報.名 = ConvertHTMLtoText(txt_名.Text);
        if (ddl_性別.SelectedValue == "-1")
            _応募情報.性別 = null;
        else if (ddl_性別.SelectedValue == "0")
            _応募情報.性別 = false;
        else
            _応募情報.性別 = true;
        _応募情報.生年月日 = Convert.ToDateTime(txt_生年月日 .Text);
        _応募情報.国籍 = Convert.ToInt32(ddl_国籍.SelectedValue);
        _応募情報.出身地 = ConvertHTMLtoText( txt_出生地.Text);
        _応募情報.ID番号 = ConvertHTMLtoText( txt_ID番号.Text);
        _応募情報.ID番号発行日 = Convert.ToDateTime(txt_ＩＤ番号発行日.Text); 
        _応募情報.発行機関 = ConvertHTMLtoText(txt_発行機関.Text);
        _応募情報.宗教 = ConvertHTMLtoText(txt_宗教.Text);
        _応募情報.民族 = ConvertHTMLtoText(txt_民族.Text);
        if (ddl_最終学歴.SelectedValue != "-1")
        {
            _応募情報.最終学歴 = Convert.ToString(ddl_最終学歴.SelectedValue);
        }
        

        return _応募情報;
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Convert character html to text
    /// </summary>
    /// <param name="temp"></param>
    /// <returns></returns>
    public string ConvertHTMLtoText(string temp)
    {
        temp = temp.Replace("<", "&lt;");
        temp = temp.Replace(">", "&gt;");
        temp = temp.Replace(Convert.ToString((char)13), "<br/>");
        return temp;
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Convert character text to html
    /// </summary>
    /// <param name="temp"></param>
    /// <returns></returns>
    public string ConvertTexttoHTML(string temp)
    {
        temp = temp.Replace("&lt;", "<");
        temp = temp.Replace("&gt;", ">");
        return temp;
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Check update data table 学歴情報登録
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public int frmJ_TraineeResumeAInput_Update(int id)
    {
        応募情報 _応募情報 = Get応募情報(id);
        if (_応募情報Manager.frmJ_TraineeResumeAInput_Update(_応募情報) == 1)
            return 1;
        return 0;
    }
}
